//获取应用实例
const app = getApp();
var utilMd5 = require('../util/md5.js');

Page({
	data: {
    bindwechatAccount:'当前尚未绑定任何支付账号',
    codeId:'',//验证码
    name:'',//姓名
    accountId:'',//微信号
    codename: '获取验证码',
  },
	onLoad: function() {
    if(app.globalData.alipay_account){
      this.setData({
        bindwechatAccount:"当前绑定支付宝"+app.globalData.alipay_account
      })
    }
  },

  codeInput: function (e) {
    var that = this;
    that.setData({
      codeId: e.detail.value
    })
  },
  nameInput: function (e) {
    var that = this;
    that.setData({
      name: e.detail.value
    })
  },
  accountInput: function (e) {
    var that = this;
    that.setData({
      accountId: e.detail.value
    })
  },

//获取验证码
  getcode(){
    var _this=this;
    var timestamp = Date.parse(new Date());
    var code = '999dmooohuikexiong999data_typeJSONsign_typehkx.userDraw.sendAlipayCodetimestamp' + timestamp/1000+'token'+app.globalData.token+'versionV1999dmooohuikexiong999';
    var MD5code = utilMd5.md5(code); //code需要加密的数据
    wx.request({
      url: app.globalData.api + '/app.php?c=UserDrawApply&a=sendAlipayCode',
      data: {
        'token': app.globalData.token,//手机号码
        'data_type':'JSON',
        'sign_type':'hkx.userDraw.sendAlipayCode',
        'version':'V1',
        'timestamp':timestamp/1000,
        'sign':''+MD5code.toUpperCase()
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if(res.data.code==1){
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              _this.setData({
                codename: '重新发送',
                disabled: false
              })

            } else {
              _this.setData({
                codename: num + "s"
              })
            }
          }, 1000)
        }
        wx.showToast({
          title: '' + res.data.msg,//提示文字
          duration: 2000,//显示时长
          mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon: 'success', //图标，支持"success"、"loading"  
          success: function () { },//接口调用成功
          fail: function () { },  //接口调用失败的回调函数  
          complete: function () { } //接口调用结束的回调函数  
        })
        
      }
    })
  },


//绑定微信
  bindWechat:function(){
    var that=this;
    var timestamp = Date.parse(new Date());
    var code = '999dmooohuikexiong999alipay_account'+this.data.accountId+'code'+this.data.codeId+'data_typeJSONsign_typehkx.userDraw.changeAlipaytimestamp' + timestamp/1000+'token'+app.globalData.token+'truename'+this.data.name+'versionV1999dmooohuikexiong999';
    var MD5code = utilMd5.md5(code); //code需要加密的数据
    wx.request({
      url: app.globalData.api + '/app.php?c=UserDrawApply&a=changeAlipay',
      data: {
        "token": app.globalData.token,//手机号码
        "alipay_account":this.data.accountId,
        "truename":this.data.name,
        'code':this.data.codeId,
        'data_type':'JSON',
        'sign_type':'hkx.userDraw.changeAlipay',
        'version':'V1',
        'timestamp':timestamp/1000,
        'sign':''+MD5code.toUpperCase()
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
        if(res.data.code==0){
          setTimeout(() => {
            wx.navigateBack({
              delta: 0,
            })
            wx.navigateBack({
              delta: 0,
            })
          }, 200);
        }
        console.log(res)
      }
    })
  },
});
